package com.example.demo.service.imp;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.Employee;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImp implements UserService {
    @Resource
    UserMapper userMapper;


    @Override
    public Object selectByUsername(String username) {
        return userMapper.selectByUsername(username);
    }

    @Override
    public void insert(User user) {
        userMapper.insert(user);
    }

    @Override
    public void updateById(User user) {
        userMapper.updateById(user);
    }

    @Override
    public void deleteById(Long id) {
        userMapper.deleteById(id);
    }

    @Override
    public Page<User> selectPage(Integer pageNum, Integer pageSize, String search) {
        LambdaQueryWrapper<User> wrapper = Wrappers.<User>lambdaQuery();
        if(StrUtil.isNotBlank(search)){
            wrapper.eq(User::getId,search).or();
            wrapper.like(User::getUsername, search).or();
            wrapper.like(User::getNickName, search).or();
            wrapper.eq(User::getDateOfBirth, search).or();
            wrapper.eq(User::getGender, search).or();
            wrapper.eq(User::getPhoneNumber, search).or();
            wrapper.eq(User::getAddress, search).or();
            wrapper.eq(User::getCreatedTime, search).or();
            wrapper.eq(User::getUpdatedTime, search).or();

        }
        return userMapper.selectPage(new Page<>(pageNum, pageSize),wrapper);
    }

    @Override
    public User loginUser(String username) {
        return userMapper.loginUser(username);
    }

    @Override
    public void updatePassowrd(User user) {
        userMapper.updatePassowrd(user);
    }

    @Override
    public int getPassowrd(int id, String password) {
        User user = userMapper.selectById(id);
        if (user.getPassword().equals(password)) {
            return 0;
        } else {
            return 1;
        }
    }
}
